package com.jxl.demo.control;

import com.jxl.demo.pojo.ChangeInfo;
import com.jxl.demo.pojo.Feedback;
import com.jxl.demo.pojo.Result;
import com.jxl.demo.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")

public class Controller {
    @Autowired(required = false) //初始化对象
    private JdbcTemplate jdbc;


    //学生使用stuNumber和pwd登录，登录成功即返回所有信息
    //http://192.168.137.139:8080/Login
    @PostMapping("/Login")
    public Result Login(@RequestBody UserInfo userInfo){
        Result result = new Result(); // 封装结果类
        UserInfo userinfos = null; // 储存后端响应数据，有对象直接返回，否则返回空
        try {
            userinfos = jdbc.queryForObject(
                    "select * from userinfo where stuNumber = ? and pwd = ?",
                    new BeanPropertyRowMapper<>(UserInfo.class),
                    userInfo.getStuNumber(),
                    userInfo.getPwd()
            );
            if (userinfos != null) {
                result.setCode(200);
                result.setResult(userinfos);
                System.out.println("学生登录成功");
            } else {
                result.setCode(401); //（未授权）
                result.setResult("学生账号或密码不正确");
                System.out.println("学生账号或密码不正确");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace(); // 打印异常
            result.setCode(500);
            result.setResult("异常: " + e.getMessage());
            return result;
        }
    }

    //学生修改密码
    //http://192.168.137.139:8080/changePwd
    @PostMapping("/changePwd")
    public Result changePwd(@RequestBody ChangeInfo changeInfo) {
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update userinfo set pwd = ? where stuNumber = ? and pwd = ?",
                    changeInfo.getNewPwd(),
                    changeInfo.getUserInfo().getStuNumber(),
                    changeInfo.getUserInfo().getPwd()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("修改密码成功");
                System.out.println("学生修改密码成功");
            } else {
                result.setCode(401); // 使用适当的HTTP状态码（未授权）
                result.setResult("旧密码错误或学生信息不存在");
                System.out.println("旧密码错误或学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //学生修改信息 gender,address,tel,nationality，politicalStatus,使用stuNumber判断
    //http://192.168.137.139:8080/changeInfo
    @PostMapping("/changeInfo")
    public Result changeInfo(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update userinfo set gender = ?, address = ?, tel = ?, nationality = ?, politicalStatus = ?, " +
                            "hobby = ?, college = ?, major = ?, age = ?, dormitory = ? where stuNumber = ?",
                    userinfo.getGender(),
                    userinfo.getAddress(),
                    userinfo.getTel(),
                    userinfo.getNationality(),
                    userinfo.getPoliticalStatus(),
                    userinfo.getHobby(),
                    userinfo.getCollege(),
                    userinfo.getMajor(),
                    userinfo.getAge(),
                    userinfo.getDormitory(),
                    userinfo.getStuNumber()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("信息修改成功");
                System.out.println("学生信息修改成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }

            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //用姓名和身份证查询学号和密码
    //http://192.168.137.139:8080/queryStuNumber
    @PostMapping("/queryStuNumber")
    public Result queryIdNumber(@RequestBody UserInfo userInfo) {
        Result result = new Result();
        try {
            UserInfo user = jdbc.queryForObject("SELECT name,stuNumber,idNumber,pwd FROM userinfo WHERE name = ? AND idNumber = ?",
                    new BeanPropertyRowMapper<>(UserInfo.class), userInfo.getName(), userInfo.getIdNumber());
            if (user != null) {
                result.setCode(200);
                result.setResult(user);
                System.out.println("姓名和身份证查询学号和密码成功");
            } else {
                result.setCode(404);
                result.setResult("不存在此用户");
                System.out.println("不存在此用户");
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //学生格言修改
    //http://192.168.137.139:8080/changeMotto
    @PostMapping("/changeMotto")
    public Result changeMotto(@RequestBody UserInfo userInfo) {
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update userinfo set motto = ? where stuNumber = ?",
                    userInfo.getMotto(),
                    userInfo.getStuNumber()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("修改格言成功");
                System.out.println("修改格言成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //学生意见反馈
    //http://192.168.137.139:8080/doFeedback
    @PostMapping("/doFeedback")
    public Result Feedback(@RequestBody Feedback feedback) {
        Result result = new Result();
        try {
            jdbc.update("INSERT INTO feedback (tel, suggestion, time) VALUES (?, ?, NOW());",
                    feedback.getTel(), feedback.getSuggestion());
            result.setCode(200);
            result.setResult("反馈提交成功");
            System.out.println("反馈提交成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
        }
        return result;
    }

    //使用stuNumber返回用户所有信息
    //http://192.168.137.139:8080/getAllInfo
    @PostMapping("/getAllInfo")
    public Result getAllInfo(@RequestBody UserInfo userInfo){
        Result result = new Result(); // 封装结果类
        UserInfo userinfos = null; // 储存后端响应数据，有对象直接返回，否则返回空
        try {
            userinfos = jdbc.queryForObject(
                    "select * from userinfo where stuNumber = ?",
                    new BeanPropertyRowMapper<>(UserInfo.class),
                    userInfo.getStuNumber()
            );
            if (userinfos != null) {
                result.setCode(200);
                result.setResult(userinfos);
                System.out.println("使用stuNumber返回用户所有信息");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace(); // 打印异常
            result.setCode(500);
            result.setResult("异常: " + e.getMessage());
            return result;
        }
    }

    //存入图片路径，使用stuNumber判断
    //http://192.168.137.139:8080/storeImage
    @PostMapping("/storeImage")
    public Result storeImage(@RequestBody UserInfo userinfo){
        Result result = new Result();
        try {
            int rowsAffected = jdbc.update(
                    "update userinfo set imgAddress = ? where stuNumber = ?",
                    userinfo.getImgAddress(),
                    userinfo.getStuNumber()
            );
            if (rowsAffected > 0) {
                result.setCode(200);
                result.setResult("图片地址存入成功");
                System.out.println("图片地址存入成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }

    //获得图片路径，使用stuNumber判断
    //http://192.168.137.139:8080/getImage
    @PostMapping("/getImage")
    public Result getImage(@RequestBody UserInfo userinfo){
        Result result = new Result();
        UserInfo userinfos = null;
        try {
            userinfos = jdbc.queryForObject(
                    "select * from userinfo where stuNumber = ?",
                    new BeanPropertyRowMapper<>(UserInfo.class),
                    userinfo.getStuNumber()
            );
            if (userinfos != null) {
                result.setCode(200);
                result.setResult(userinfos);
                System.out.println("图片地址获得成功");
            } else {
                result.setCode(404); // 使用适当的HTTP状态码（未找到）
                result.setResult("学生信息不存在");
                System.out.println("学生信息不存在");
            }
            return result;
        } catch (DataAccessException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setResult("其他错误");
            return result;
        }
    }


}
